package com.gitee.ywj1352.jdk8;

import java.util.Observable;
import java.util.Observer;

public class Publish extends Observable {

    public String getData(){
        return "11";
    }

    public void setData(String data){
        System.out.println(data);
        super.setChanged();
        //实际上 是Observable 保存了引用 观察者的引用
        super.notifyObservers();
    }


    public static class Subscribe implements Observer{
        //通知 观察着的方法
        @Override
        public void update(Observable o, Object arg) {
            System.out.println("收到通知:" + ((Publish)o).getData());
        }
    }


    public static void main(String[] args) {
        Subscribe subscribe = new Subscribe();
        Publish publish = new Publish();
        publish.addObserver(subscribe);
        publish.setData("开始");
    }






}
